import defend from '../utils/Token'

import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

import Home from '../components/Home.vue'
import News from '../components/News.vue'

import List from '../components/List.vue'

import NotFound from '../components/NotFound.vue'

import WuHan from '../components/WuHan.vue'

import YiChang from '../components/YiChang.vue'

import User from '../components/User.vue'

import Login from '../components/Login.vue'

import Set from '../components/Set.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        // { path: '/home', redirect: '/' }
        { path: '/', component: Home },
        {
            path: '/home', name: 'home', component: Home,
            meta: {
                title: '首页'
            }
        },
        // { path: '/news', name: 'news', component: News },
        // 动态路由
        { path: '/news/:id', name: 'news', component: News, props: true },
        {
            path: '/list', name: 'list', component: List, props: { age: 120 },
            meta: {
                title: '列表',
                PermissionStatus: true,

            }

        },
        //  方式一 啥都不写
        // { path: '/list/:id/:name', name: 'list', component: List }
        //  方式二  添加props
        // { path: '/list/:id/:name', name: 'list', component: List, props: true }
        // 方式三 合并数据
        // { path: '/list/:id/:name', name: 'list', component: List, props: { age: 100 } }
        {
            path: '/list/:id/:name', name: 'list', component: List, props: route => ({
                id: route.params.id,
                name: route.params.name,
                age: 100
            })
        },


        {

            path: '/user', name: 'user', component: User,
            meta: {
                title: '用户',
                PermissionStatus: true
            },
            children: [
                {
                    path: '', component: WuHan,

                },
                {
                    path: 'wuhan', name: 'wuhan', component: WuHan,

                },
                {
                    path: 'yichang', name: 'yichang', component: YiChang,
                }
            ]
        },

        {
            path: '/login', name: 'login', component: Login,
            meta: {
                title: '登录页',
                PermissionStatus: false
            }
        },


        {
            path: '/set', name: 'set', component: Set
        },

        { path: '/:pathMatch(.*)*', component: NotFound },


    ],
    scrollBehavior (to, from, savedPosition) {

        console.log(savedPosition)
        if (savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0, y: 0
            }
        }

    }
})




// console.log(defend.isLogin())

router.beforeEach((to, from, next) => {
    //  不管有没有登录 如果直接去到登录页面 那么放行 
    if (to.path === '/login') {
        next()
    } else { // 去的是不登录页面
        //  1 判断 有没有登录  如果登陆了 那么直接放行 可以去到
        // 别的的页面  
        // 2 如果没有登录 跳转到登录页面  重新进行登录
        if (defend.isLogin()) {
            next()
        } else {
            alert('没有登录')
            return next('/login')
        }

    }
})





// router.beforeEach((to, from, next) => {
//     if (to.name !== 'Login' && !defend.isLogin()) next({ name: 'Login' })
//     else next()
// })


function setTitle (title) {
    document.title = title
}


router.afterEach((to, from, failure) => {
    console.log(to.meta);
    // console.log(failure);
    if (!failure) setTitle(to.meta.title)
})

export default router


